package com.hssy.wululuaplan.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hssy.wululuaplan.common.ExceptionCodeEnum;
import com.hssy.wululuaplan.common.SystemProperties;
import com.hssy.wululuaplan.entity.InviteCodeRecord;
import com.hssy.wululuaplan.entity.SysUser;
import com.hssy.wululuaplan.exception.CustomException;
import com.hssy.wululuaplan.form.RegisterForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 注册 业务类
 * @since 2025/6/19 17:30
 * @author Mr.Hongtao
 */
@Service
public class RegisterService {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SystemProperties systemProperties;
    @Resource
    private InviteCodeRecordService inviteCodeRecordService;

    @Transactional(rollbackFor = Exception.class)
    public void register(RegisterForm form) {
        // 1. 校验用户信息
        String username = form.getUsername();
        String password = form.getPassword();
        password = DigestUtils.md5Hex(password + systemProperties.getSlat());
        SysUser one = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery()
                .eq(SysUser::getUsername, username)
                .eq(SysUser::getPassword, password)
                .last("limit 1"));
        if (one != null) {
            throw new CustomException(ExceptionCodeEnum.USERNAME_EXIST);
        }
        // 2. 校验验证码
        InviteCodeRecord record = inviteCodeRecordService.getOne(Wrappers.<InviteCodeRecord>lambdaQuery()
                .eq(InviteCodeRecord::getInviteCode, form.getInviteCode())
                .eq(InviteCodeRecord::getStatus, true)
                .ge(InviteCodeRecord::getExpireTime, new Date())
                .last("limit 1"));
        if (record == null) {
            throw new CustomException(ExceptionCodeEnum.INVITE_CODE_ERROR);
        }
        // 3. 注册用户
        SysUser user = new SysUser();
        user.setUsername(username);
        user.setPassword(password);
        sysUserService.save(user);
        // 4. 更新邀请码状态
        record.setStatus(false);
        record.setExpireTime(new Date());
        inviteCodeRecordService.updateById(record);
    }

    public InviteCodeRecord createInviteCode(String token, Integer expiry) {
        // 1. 校验token
        Integer userId = sysUserService.getUserId(token);
        if (userId == null) {
            throw new CustomException(ExceptionCodeEnum.LOGIN_EXPIRED);
        }
        // 2. 创建邀请码
        return inviteCodeRecordService.createInviteCode(userId,expiry);
    }

    public List<InviteCodeRecord> getInviteCodeList(String token) {
        // 1. 校验token
        Integer userId = sysUserService.getUserId(token);
        if (userId == null) {
            throw new CustomException(ExceptionCodeEnum.LOGIN_EXPIRED);
        }
        // 2. 获取全部验证码
        return inviteCodeRecordService.list(Wrappers.<InviteCodeRecord>lambdaQuery()
                .eq(InviteCodeRecord::getUserId, userId)
                .orderByDesc(InviteCodeRecord::getStatus)
                .orderByDesc(InviteCodeRecord::getExpireTime));
    }
}
